package com.wilmer.TecherLessonPlan.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.wilmer.TecherLessonPlan.domain.vo.LessonPlan;
import com.wilmer.TecherLessonPlan.mapper.YwLessonPlanMapper;
import com.wilmer.common.annotation.Anonymous;
import com.wilmer.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.TecherLessonPlan.domain.YwLessonPlan;
import com.wilmer.TecherLessonPlan.service.IYwLessonPlanService;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.common.core.page.TableDataInfo;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 教案管理Controller
 *
 * @author hans
 * @date 2024-04-27
 */

@Api(tags = "教案管理，可以AI生成教案，可以手动写教案，教案可修改")
@RestController
@RequestMapping("/TecherLessonPlan/TecherLessonPlan")
public class YwLessonPlanController extends BaseController
{
    @Autowired
    private IYwLessonPlanService ywLessonPlanService;
    @Autowired
    private YwLessonPlanMapper ywLessonPlanMapper;

    /**
     * 查询教案管理列表
     */
    @ApiOperation("获取教案列表，支持分页默认10个")
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(YwLessonPlan ywLessonPlan)
    {
        startPage();
        List<YwLessonPlan> list = ywLessonPlanService.selectYwLessonPlanList(ywLessonPlan);
        return getDataTable(list);
    }
    @GetMapping("/666")
    public YwLessonPlan aiInsertYwLessonPlan(){


        YwLessonPlan ywLessonPlan=new YwLessonPlan();
        ywLessonPlan.setUserId(SecurityUtils.getUserId());
        ywLessonPlan.setDeptId(SecurityUtils.getDeptId());
        ywLessonPlan.setCreateBy("AI小默");
        ywLessonPlan.setCreateTime(new Date());
        ywLessonPlan.setLplanName("9999");
        ywLessonPlan.setLplanMain("lesson");

        System.out.println(ywLessonPlanMapper.insertYwLessonPlan(ywLessonPlan));

        return ywLessonPlanMapper.aiInsertYwLessonPlan(ywLessonPlan);
//        ywLessonPlanMapper.insertYwLessonPlan("");
    }
    /**
     * 查询教案管理列表
     */
    @ApiOperation("Ai生成教案，生成方式知识库+openai")
    @PostMapping(value = "/kkk")
    public SseEmitter createLessonPlan(@RequestBody LessonPlan lessonPlan) {
        System.out.println(lessonPlan+"-----");
        String uid=  UUID.randomUUID().toString();
        //建立客户端连接
        SseEmitter sse = ywLessonPlanService.createSse(uid);
         ywLessonPlanService.createLessonPlan(lessonPlan,uid);
        return sse;
    }

    @ApiOperation("建立连接")
    @Anonymous
    @GetMapping ("/createSSe")
    public SseEmitter createLogin()
    {
        //建立客户端连接
        SseEmitter sse = ywLessonPlanService.createSse(SecurityUtils.getUserId().toString());
        return sse;
    }

    /**
     * 导出教案管理列表
     */
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:export')")
    @Log(title = "教案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YwLessonPlan ywLessonPlan)
    {
        List<YwLessonPlan> list = ywLessonPlanService.selectYwLessonPlanList(ywLessonPlan);
        ExcelUtil<YwLessonPlan> util = new ExcelUtil<YwLessonPlan>(YwLessonPlan.class);
        util.exportExcel(response, list, "教案管理数据");
    }
    /**
     * 获取教案管理详细信息
     */
    @ApiOperation("根据id获取教案")
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:query')")
    @GetMapping(value = "/{lplanId}")
    public AjaxResult getInfo(@PathVariable("lplanId") Long lplanId)
    {
        return success(ywLessonPlanService.selectYwLessonPlanByLplanId(lplanId));
    }

    /**
     * 新增教案管理
     */
    @ApiOperation("新增教案，教师自己写教案")
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:add')")
    @Log(title = "教案管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YwLessonPlan ywLessonPlan)
    {
        return toAjax(ywLessonPlanService.insertYwLessonPlan(ywLessonPlan));
    }

    /**
     * 修改教案管理
     */
    @ApiOperation("修改教案")
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:edit')")
    @Log(title = "教案管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YwLessonPlan ywLessonPlan)
    {
        return toAjax(ywLessonPlanService.updateYwLessonPlan(ywLessonPlan));
    }

    /**
     * 删除教案管理
     */
    @ApiOperation("删除教案")
    @PreAuthorize("@ss.hasPermi('TecherLessonPlan:TecherLessonPlan:remove')")
    @Log(title = "教案管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lplanIds}")
    public AjaxResult remove(@PathVariable Long[] lplanIds)
    {
        return toAjax(ywLessonPlanService.deleteYwLessonPlanByLplanIds(lplanIds));
    }
}
